package org.jeecg.modules.api.xxApi.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.api.xxApi.vo.*;
import org.jeecg.modules.apiacl.controller.ApiAuthorizeController;
import org.jeecg.modules.innerApi.common.requestDto.CurrentFphmParam;
import org.jeecg.modules.innerApi.common.responseDto.FaceImgRtn;
import org.jeecg.modules.xxgx.common.service.IXxBase;
import org.jeecg.modules.xxgx.common.vo.XxApiComSearchParam;
import org.jeecg.modules.xxgx.common.vo.XxApiZnfmParam;
import org.jeecg.modules.xxgx.fpgl.dto.XxApifpDownloadDto;
import org.jeecg.modules.xxgx.fpgl.vo.XxApifpDownloadParam;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 销项基础接口：智能赋码、云抬头检索
 * @Author: jeecg-boot
 * @Date: 2023-08-02
 * @Version: V1.0
 */
@ApiSort(value = 2)
@Api(tags = "销项接口管理")
@RestController
@RequestMapping(CommonConstant.API+"/xxApi")
@Slf4j
public class XxBaseApiController extends ApiAuthorizeController {

    @Autowired
    private IXxBase xxBase;

    /**
     * 销项-智能赋码接口
     *
     * @param xxApiZnfmParam
     * @return
     */
    @AutoLog(value = "智能赋码")
    @ApiOperation(value = "智能赋码", notes = "智能赋码",position = 1)
    @PostMapping(value = "/znfm")
    public Result<JSONObject> znfm(@RequestBody XxApiZnfmParam xxApiZnfmParam) {
        String spmc = xxApiZnfmParam.getSpmc();
        log.info(xxApiZnfmParam.toString());
        JSONObject dataJson = new JSONObject();
        Result result = null;
        result = xxBase.znfm(spmc);
        if (result.isSuccess()) {
            dataJson.put("groups", result.getResult());
            result.setResult(dataJson);
        }
        return result;
    }

    /**
     * 销项-云抬头检索接口
     *
     * @param searchParam
     * @return
     */
    @AutoLog(value = "云抬头检索")
    @ApiOperation(value = "云抬头检索", notes = "云抬头检索",position = 2)
    @PostMapping(value = "/comSearch")
    public Result<JSONObject> comSearch(@RequestBody XxApiComSearchParam searchParam) {
        String searchName = searchParam.getSearchName();
        log.info(searchParam.toString());
        JSONObject dataJson = new JSONObject();
        Result result = null;
        result = xxBase.comSearch(searchName);
        if(result.isSuccess()){
            dataJson.put("groups", result.getResult());
            result.setResult(dataJson);
        }
        return result;
    }

    /**
     * 销项-获取开票终端接口
     *
     * @param param
     * @return
     */
    @AutoLog(value = "获取开票终端")
    @ApiOperation(value = "获取开票终端", notes = "获取开票终端",position = 3)
    @PostMapping(value = "/getKpzd")
    public Result<JSONObject> getKpzd(
                                      @RequestBody XxApiGetKpzdParam param) {
        Result result = null;
        result = xxBase.getKpzd(param.getNsrmc(),param.getNsrsbh());
        if(result.isSuccess()){
            //转换开票终端信息
            List<XxApiKpzdResult> csXxKpzds=((List<CsXxKpzd>)result.getResult()).parallelStream().map(kpzd -> {
                XxApiKpzdResult xxApiKpzdResult=new XxApiKpzdResult();
                BeanUtils.copyProperties(kpzd,xxApiKpzdResult);
                xxApiKpzdResult.setKpzddm(kpzd.getZddm());
                xxApiKpzdResult.setKpzddm(kpzd.getZddm());
                return xxApiKpzdResult;
            }).collect(Collectors.toList());
            JSONObject dataJson = new JSONObject();
            dataJson.put("groups",csXxKpzds);
            result.setResult(dataJson);
        }
        return result;
    }

    /**
     * 销项-获取开票终端接口
     *
     * @param param
     * @return
     */
    @AutoLog(value = "数电票平台登录")
    @ApiOperation(value = "数电票平台登录", notes = "数电票平台登录",position = 4)
    @PostMapping(value = "/loginDppt")
    public Result<JSONObject> loginDppt(@RequestBody XxApiLoginDpptParam param) {
        return xxBase.loginDppt(param.getKpzddm(),param.getDllx(),param.getVerify());
    }

       /**
     * 销项-获取数电票实名认证二维码接口
     *
     * @param param
     * @return
     */
    @AutoLog(value = "数电票实名认证二维码")
    @ApiOperation(value = "数电票实名认证二维码", notes = "数电票实名认证二维码",position = 5)
    @PostMapping(value = "/getSmrzEwm")
    public Result<JSONObject> getSmrzEwm(@RequestBody XxApiGetSmrzEwmParam param) {
        Result<JSONObject> result=xxBase.getSmrzEwm(param.getKpzddm(),param.getErmlx());
        if(result.isSuccess()){
            JSONObject JsonRtn=new JSONObject(result.getResult());
            FaceImgRtn.FaceImgRtnData imgRtn=JsonRtn.toJavaObject(FaceImgRtn.FaceImgRtnData.class);
            JSONObject rtnData=new JSONObject();
            rtnData.put("ewm",imgRtn.getEwm());
            rtnData.put("ewmlx",param.getErmlx());
            result.setResult(rtnData);
        }
        return result;
    }

    /**
     * 销项-获取数电终端认证到期时间接口
     *
     * @param param
     * @return
     */
    @AutoLog(value = "数电终端认证到期时间")
    @ApiOperation(value = "数电终端认证到期时间", notes = "数电终端认证到期时间",position = 6)
    @PostMapping(value = "/kpzdRzdqDate")
    public Result<XxApiKpzdResult> kpzdRzdqDate(@RequestBody XxApiKpzddmParam param) {
        Result result = null;
        result = xxBase.getKpzd(param.getKpzddm());
        if(result.isSuccess()){
            CsXxKpzd kpzd=(CsXxKpzd)result.getResult();
            XxApiKpzdResult xxApiKpzdResult=new XxApiKpzdResult();
            BeanUtils.copyProperties(kpzd,xxApiKpzdResult);
            xxApiKpzdResult.setKpzddm(kpzd.getZddm());
            xxApiKpzdResult.setKpzdmc(kpzd.getZdmc());
            result.setResult(xxApiKpzdResult);
        }
        return result;
    }

    /**
     * 销项-获取当前发票号码接口
     *
     * @param currentFphmParam
     * @return
     */
    @AutoLog(value = "获取当前发票号码")
    @ApiOperation(value = "获取当前发票号码", notes = "获取当前发票号码",position =7 )
    @PostMapping(value = "/currentFphm")
    public Result currentFphm(@RequestBody CurrentFphmParam currentFphmParam) {
        log.info(currentFphmParam.toString());
        return xxBase.currentFphm(currentFphmParam);
    }

    /**
     * 销项-获取数电票授信额度接口
     *
     * @param kpzddm
     * @return
     */
    @AutoLog(value = "获取数电票授信额度")
    @ApiOperation(value = "获取数电票授信额度", notes = "获取数电票授信额度",position = 8)
    @PostMapping(value = "/getCredit")
    public Result<JSONObject> getCredit(@RequestBody XxApiKpzddmParam kpzddm) {
        return xxBase.getCredit(kpzddm.getKpzddm());
    }

    @AutoLog(value = "电子发票版式下载")
    @ApiOperation(value = "电子发票版式下载", notes = "电子发票版式下载",position = 9)
    @PostMapping(value = "/fpDownload")
    public Result<XxApifpDownloadDto> fpDownload(@RequestBody @Valid XxApifpDownloadParam param, BindingResult bindingResult) {
        StringBuilder errMsg = new StringBuilder();
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                String defaultMessage = objectError.getDefaultMessage();
                errMsg.append(defaultMessage).append(",");
            }
        }
        if(errMsg.length() > 0){
            throw new JeecgBootException(errMsg.toString());
        }
        return xxBase.fpDownload(param);
    }


}
